<?php

if ( ! function_exists('authentication'))
{
	function generateToken($str){
		return  md5(sha1($str.time()).time());
	}
	function generatePassword($password, $token){
		return md5($password.$token);
	}
	
	function doNeedLogin($url){
		$ci = get_instance();
		//$full_url = base_url().$url;
		$ci->session->set_userdata('redirect_url',$url);
		$islogin = $ci->session->userdata('is_logged');
		if ($islogin == false) {
			redirect('admin', 'refresh');// cho nay cho direct link luon
		}
	}
	
	function isValidURL($URL){
    $exists = true;
    $file_headers = @get_headers($URL);
    $InvalidHeaders = array('404', '403', '500');
    foreach($InvalidHeaders as $HeaderVal)
    {
            if(strstr($file_headers[0], $HeaderVal))
            {
                    $exists = false;
                    break;
            }
    }
    return $exists;
	}
	
	/**
 * get youtube video ID from URL
 *
 * @param string $url
 * @return string Youtube video id or FALSE if none found. 
 */
function getYoutubeID($url) {
    $pattern = 
        '%^# Match any youtube URL
        (?:https?://)?  # Optional scheme. Either http or https
        (?:www\.)?      # Optional www subdomain
        (?:             # Group host alternatives
          youtu\.be/    # Either youtu.be,
        | youtube\.com  # or youtube.com
          (?:           # Group path alternatives
            /embed/     # Either /embed/
          | /v/         # or /v/
          | /watch\?v=  # or /watch\?v=
          )             # End path alternatives.
        )               # End host alternatives.
        ([\w-]{10,12})  # Allow 10-12 for 11 char youtube id.
        $%x'
        ;
    $result = preg_match($pattern, $url, $matches);
    if (false !== $result) {
        return $matches[1];
    }
    return false;
}


		
}

?>